package com.blog.zip.controller;

import com.blog.zip.mapper.ArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 负责处理首页需要加载的相关数据
 */
@RestController
public class IndexController {
    @Autowired
    private ArticleMapper articleMapper;


    @PostMapping("/indexData/get")
    public Map getIndexDate() {

        Map<Long, Object> result = new HashMap<>();
        //获取归档的相关信息
        List<Map> list = new ArrayList<>(), resultList = new ArrayList<>();
        list = articleMapper.getArchive();
//        result = list.get(0);
        int count = 0;
        for (Map value : list) {
            result.put((Long) value.get("archiveTime"), value);
        }
        //System.out.println(result);
        for (Long key : result.keySet()) {
            //System.out.println("key= " + key + " and value= " + result.get(key));
            int j = 0;
            for (Map map : list) {
                //System.out.println("------- result中的" + key);
                //System.out.println("-------list中的" + map.get("archiveTime"));
                //System.out.println(map.get("archiveTime") == key);
                if (map.get("archiveTime") == key) {
                    j++;

                    //System.out.println(j);
                }
            }

            result.put(key, j);
        }
        //System.out.println(result);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("archive", list);
        return map;
    }
}
